package svl;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import xml.solicitudCotizacion.SolicitudCotizacion;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

public class Parseo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	
		String xmlString = readFile("C:/xml/solicitudCotizacion.xml");
		
		 XStream xstream = new XStream(new DomDriver());
		
		 xstream.processAnnotations(SolicitudCotizacion.class);
		
		 SolicitudCotizacion cotizacion = (SolicitudCotizacion)
		 xstream.fromXML(xmlString);
		
		 request.setAttribute("cotizacion", cotizacion);
		
		 request.getRequestDispatcher("/WEB-INF/jsp/ReadXML.jsp").forward(request,
		 response);

	}

	private String readFile(String file) throws IOException {
		BufferedReader reader = new BufferedReader(new FileReader(file));
		String line = null;
		StringBuilder stringBuilder = new StringBuilder();
		String ls = System.getProperty("line.separator");

		while ((line = reader.readLine()) != null) {
			stringBuilder.append(line);
			stringBuilder.append(ls);
		}

		return stringBuilder.toString();
	}
}
